if(navigator.geolocation){
	navigator.geolocation.getCurrentPosition(successCallback,errorCallback);
}else{
	alert("your Browser is not suport geolocation!");
}

function successCallback(position) {
	var nowLatitude = Math.round(position.coords.latitude*1000000);  
    var nowLongitude = Math.round(position.coords.longitude*1000000);
    var jsonbody = {};
    jsonbody["param"]=",,,"+nowLatitude+","+nowLongitude;
    jsonbody["hl"]=window.localStorage.getItem("url")?window.localStorage.getItem("url"):"en";
    var body=JSON.stringify(jsonbody);
    xhr('POST', 'html5-show/weather', body,setweather);
}

function errorCallback(error) {
 console.log(error);
}

function xhr(method, uri, body, handler,searchinput) {
    var req = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
    req.onreadystatechange = function() {
        if (req.readyState == 4 && handler) {
        	if (req.status == 200) {
        	    var result = JSON.parse(req.responseText);
                handler(result,searchinput);
            } else {
            	 document.location.href = document.location.toString();
            }
        } 
    }
    req.open(method, uri, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.send(body);
}
var tem_f,tem_c;
function setweather(weather,searchinput){
	console.log(weather);
	var weatherlist=weather.list;
	var map=weather.map;
	var hl=weather.hl;
	var map_w;var map_s;
	if(map.wind_condition.indexOf("、")==-1){
		map_w=map.wind_condition.split("at")[0];
		map_s=map.wind_condition.split("at")[1];
	}else{
		map_w=map.wind_condition.split("、")[0];
		map_s=map.wind_condition.split("、")[1];
	}
	//var maphu=(map.humidity).split(":");
	tem_f=map.temp_f;
	tem_c=map.temp_c;
	var i;
	var object;
	var str="";
	var weatherstr="";
	var curr_weather="";
	//$("#weather_introduction").html('<div class="weather_icon"><img src="image/tools_weather/'+map.icon.split("/")[4].split(".")[0]+'.png"/></div><div class="detailed_introduction"><div class="left_temperature"><p><p class="temperature">'+map.temp_c+'</p> <p class="deg temperature">&deg;</p></p><div id="temperature_button"><a href="#" id="f">&deg;F</a><a href="#" id="c">&deg;C</a></div></div><div class="weather_situation"><p class="cloudy">'+map.condition+'</p><ul class="weather_list"><li class="text humidity">'+map.humidity+'</li><li class="text wind_direction">'+map_w+'</li><li class="text wind_velocity">'+map_s+'</li></ul></div></div>');
	$("#weather_introduction").html('<div class="weather_icon"><img src="image/tools_weather/'+map.icon.split("/")[4].split(".")[0]+'.png"/></div><div class="detailed_introduction"><div class="left_temperature"><p><p class="degree temperature" id="temp">'+map.temp_c+'</p><p class="deg temperature">&deg;</p></p><div id="temperature_button"><a href="#" id="f" class="c" onClick="changTemp(f)">&deg;F</a><a href="#" id="c" class="f" onClick="changTemp(c)">&deg;C</a></div></div><div class="weather_situation"><p class="cloudy">'+map.condition+'</p><ul class="weather_list"><li class="text humidity">'+map.humidity+'</li><li class="text wind_direction">'+map_w+'</li><li class="text wind_velocity">'+map_s+'</li></ul></div></div>');
	$("#weatherlist").html("");
	for(i=0;i<weatherlist.length;i++){
		object=weatherlist[i];
		str='<li><ul class="weatherIn_one_columns weatherIn_four_columns"><li class="weather_font weather_week">'+object.day_of_week+'</li><li class="small_icon"><img src="image/tools_weather/'+object.icon.split("/")[4].split(".")[0]+'.png" width="70" height="52"/></li>'+
			'<li class="weather_font weather_condition">'+object.condition+'</li><li class="weather_font air_temperature" id="temp'+i+'">'+(hl=="en"?Math.round((object.low-32)*5/9):object.low)+"&deg~"+(hl=="en"?Math.round((object.high-32)*5/9):object.high)+'&deg</li></ul></li>';
		weatherstr+=str;
	}
	if(weatherlist.length>0)
		$("#city").html(searchinput);
	$("#weatherlist").html(weatherstr);
}

function changTemp(e){ 
	var id=e.id;
	if("f"==id){
		
		if(tem_f==($("#temp").text())) 
			return;
		$("#temp").html(tem_f);
		$("#f").removeClass("c").addClass("f");
		$("#c").removeClass("f").addClass("c");
		for(var i=0;i<4;i++){
			var temparray=($("#temp"+i).text()).replace("°","").replace("°","").split("~");
			var templow=temparray[0];
			var temphigh=temparray[1];
			$("#temp"+i).html(Math.round(templow*9/5+32)+"&deg~"+Math.round(temphigh*9/5+32)+"&deg");
			
		}
	}else{
		
		if(tem_c==($("#temp").text()))
			return;
		$("#temp").html(tem_c);
		//$("#f").addClass("c");
		$("#c").removeClass("c").addClass("f");
		$("#f").removeClass("f").addClass("c");
		for(var i=0;i<4;i++){
			var temparray=($("#temp"+i).text()).replace("°","").replace("°","").split("~");
			var templow=temparray[0];
			var temphigh=temparray[1];
			$("#temp"+i).html(Math.round((templow-32)*5/9)+"&deg~"+Math.round((temphigh-32)*5/9)+"&deg");
		}
	}	
}
function searchbykeyword(){
	var searchinput=$("#text_box").val();
	var jsonbody = {};
    jsonbody["param"]=searchinput;
    jsonbody["hl"]=window.localStorage.getItem("url")?window.localStorage.getItem("url"):"en";
    var body=JSON.stringify(jsonbody);
	xhr('POST', 'html5-show/weather', body,setweather,searchinput);
}

$(function(){
	$("#click_seach1").click(function(){
		var searchinput=$("#text_box1").val();
		if(equipment.indexOf("phone")!=-1)
			hidephone();
		var jsonbody = {};
	    jsonbody["param"]=searchinput;
	    jsonbody["hl"]=window.localStorage.getItem("url")?window.localStorage.getItem("url"):"en";
	    var body=JSON.stringify(jsonbody);
		xhr('POST', 'html5-show/weather', body,setweather,searchinput);
    })
})

$(function(){
	$("#click_seach").click(function(){
		if(equipment.indexOf("phone")!=-1){
			showphone();
		}else if(equipment=="pad"){
			clickonpad();	
		}else{
			searchbykeyword();
		}
	})
})

function showphone(){
	$("#light").show();
    $("#fade").show();
}

function hidephone(){
	$("#light").hide();
    $("#fade").hide();
}

function clickonpad(){
	 $("#right_searchbox").hide();
	 $("#light").show();
	 document.getElementById('light').style.right = '-3px';
}

function androidphoneadddiv(){
	if(equipment=="androdiphone"){
		document.getElementById("weather_container").className="androdiphone";
	}
	
}

	


